import Vue from 'vue'
import VueRouter from 'vue-router'
//
import { Loading } from 'element-ui';
//
import Main from '../../views/Main'
import Login from '../../views/Login'
//
import Home from '../../views/Home'
//Base
import WangEditor from '../../views/Base/WangEditor'
import Curd from '../../views/Base/Curd'
import Calendar from '../../views/Base/Calendar'
import Neditor from '../../views/Base/Neditor'
//
Vue.use(VueRouter)

const routers = [{
    path: '/',
    component: Main,
    children: [{
        path: '/Home',
        name: "/Home",
        component: Home,
        meta: { title: '首页' },
    }, {
        path: '/Curd',
        name: '/Curd',
        component: Curd,
        meta: { title: '列表页面' },
    }, {
        path: '/WangEditor',
        name: '/WangEditor',
        component: WangEditor,
        meta: { title: 'WangEditor编辑器' },
    }, {
        path: '/Neditor',
        name: '/Neditor',
        component: Neditor,
        meta: { title: 'Neditor编辑器' },
    }, {
        path: '/Calendar',
        name: '/Calendar',
        component: Calendar,
        meta: { title: '全屏日历' },
    }]
},
{ path: '/Login', name: 'Login', component: Login },
{ path: '*', redirect: "/Home" }
];

const vueRouter = new VueRouter({
    routes: routers
});
let _loading = null;
//监听路由
vueRouter.beforeEach((to, from, next) => {
    console.log('路由拦截器', from);
    _loading = Loading.service({
        lock: true,
        background: 'rgba(0, 0, 0, 0.1)'
    });
    //路由拦截器
    if (to.meta.hasOwnProperty('title')) {
        global.$store.commit('app/addOrCheckedTab', {
            title: to.meta.title,
            name: to.name,
            active: true
        });
    }
    next()
});
vueRouter.afterEach(to => {
    if (_loading) _loading.close();
});

export default vueRouter;